<template>
  <div class="home-page">
    <h2>{{ biggerColumnLength }}</h2>
    <section class="py-5 text-center container">
      <div class="row py-lg-5">
        <div class="col-lg-6 col-md-8 mx-auto">
          <img src="@/assets/callout.svg" alt="callout" class="w-50">
          <h2 class="font-weight-light">随心写作，自由表达</h2>
          <p>
            <router-link to="/create"><a href="#" class="btn btn-primary my-2">开始写文章</a></router-link>
          </p>
        </div>
      </div>
    </section>
    <uploader
      action="/upload"
      :before-upload="beforeUpload"
      @file-uploaded="onFileUploaded"
    >
      <template #uploaded="dataProps">
        <img :src="dataProps.uploadedData.data.url" width="5000">
      </template>
    </uploader>
    <h4 class="font-weight-bold text-center">发现精彩</h4>
    <column-list :list="list"></column-list>
    <button
      class="btn btn-outline-primary mt-2 mb-5 mx-auto btn-block w-25 more"
      @click="loadMorePage" v-if="!isLastPage"
    >
      加载更多
    </button>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { GlobalDataProps, ResponseType, ImageProps } from '@/store'
import ColumnList from '@/components/ColumnList.vue'
import createMessage from '@/selfFunction/createMessage'
import Uploader from '@/views/Uploader.vue'
import useLoadMore from '@/selfFunction/useLoadMore'

export default defineComponent({
  name: 'Home',
  components: {
    Uploader,
    ColumnList
  },
  setup () {
    const store = useStore<GlobalDataProps>()
    const total = computed(() => store.state.columns.total)
    const currentPage = computed(() => store.state.columns.currentPage)
    onMounted(() => {
      store.dispatch('fetchColumns', { page: 3 })
    })
    const list = computed(() => store.getters.getColumns)
    const biggerColumnLength = computed(() => store.getters.biggerColumnLength)
    const {
      loadMorePage,
      isLastPage
    } = useLoadMore('fetchColumns', total, {
      currentPage: (currentPage.value ? currentPage.value + 1 : 2),
      pageSize: 3
    })
    const beforeUpload = (file: File) => {
      const isJPG = file.type === 'image/jpeg'
      if (!isJPG) {
        createMessage('上传图片只能是JPG格式!', 'error')
      }
      return isJPG
    }
    const onFileUploaded = (rawData: ResponseType<ImageProps>) => {
      createMessage(`上传图片ID ${rawData.data._id}`, 'success')
    }
    return {
      list,
      biggerColumnLength,
      beforeUpload,
      onFileUploaded,
      loadMorePage,
      isLastPage
    }
  }
})
</script>

<style scoped>
.more {
  display: flex;
  text-align: center;
  justify-content: space-around;
}
</style>
